
package tablas;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.event.*;

public class TableDemo extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable tabla;
	private JLabel lblEstado = new JLabel(" ");
	
	private TableDemo() {
        super("TableDemo");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(screenSize.width/4, screenSize.height/3, 
             		  screenSize.width/2, screenSize.height/3);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        }); 	
		configurar();
	}
	
	private void configurar() {
		String[] header = {"Articulo", "Cantidad", "Costo", "Pagado" };
		Object[][] data = { 
			{"Folio", new Integer(1000), new Float(.55f), new Boolean(false)},
			{"Sobre", new Integer(10000), new Float(.05f), new Boolean(false)},
			{"Resaltador", new Integer(150), new Float(1.10f), new Boolean(true)},
			{"Carpeta", new Integer(900), new Float(2.89f), new Boolean(true)}
		  };
			
		tabla = new JTable(data, header);
		
		/** 
		 * retorna el ListSelectionModel asociado a la tabla.
		 */
		ListSelectionModel lsm = tabla.getSelectionModel();
		lsm.addListSelectionListener(new ListSelectionListener(){
			public void valueChanged(ListSelectionEvent e) {
				 ListSelectionModel lsm = (ListSelectionModel)e.getSource();
	             if (!lsm.isSelectionEmpty()) {
	                 lblEstado.setText("Fila: " + (lsm.getMinSelectionIndex()+1) 
	                		 + " de " + tabla.getRowCount());
	             }			 
			}
		});
		
		getContentPane().add(new JScrollPane(tabla), BorderLayout.CENTER);
		getContentPane().add(lblEstado,BorderLayout.PAGE_END);
	}

	public static void main(String args[]) {
		new TableDemo().setVisible(true);
	}
}
